<?
require_once($r."_engine/mail_functions.php");


class CSqlTable
{
	var $table, $field_id, $field_types;
	
	function CSqlTable($table="", $field_id="")
	{
		$this->table = $table;
		$this->field_id = $field_id;
	}
	
	function Insert($values)
	{
		$sf=""; $sv="";	$i=0;
		foreach ($values as $f => $v)
		{
			if ($i++ > 0) { $sf.=", "; $sv.=", "; }
			$sf .= $f;
			$sv .= $this->EncodeValue($f, $v);
		}
		
		$query = "INSERT INTO ".$this->table.
			" ($sf) VALUES ($sv)";
			
		execute_query($query) or die();
	}
	
	function SelectRow($query)
	{
		$res = execute_query($query);
		if (@mysql_num_rows($res) < 1) return false;
		
		$row = @mysql_fetch_array($res);
		@mysql_free_result($res);
		
		$this->DecodeValues($row);
		
		return $row;
	}
	function SelectRows($query)
	{
		$result = array();
		$res = execute_query($query);
		
		while ($row = @mysql_fetch_array($res))
		{
			$this->DecodeValues($row);
			$result[] = $row;
		}

		@mysql_free_result($res);
		
		return $result;
	}
	
	function Update($values, $ids)
	{
		return $this->UpdateWhere($values, $this->IdsToWhere($ids));
	}
	function UpdateWithoutEncoding($values, $ids)
	{
		return $this->UpdateWhere($values, $this->IdsToWhere($ids), 
			false /* don't encode values*/);
	}
	function UpdateWhere($values, $where, $encode_values=true)
	{
		$s=""; $i=0;
		foreach ($values as $f => $v)
		{
			if ($i++ > 0) $s .= ", ";
			if ($encode_values) $v = $this->EncodeValue($f, $v);
			$s .= $f . "=" . $v;
		}
		
		$query = "UPDATE ".$this->table.
			" SET ".$s.
			" WHERE ".$where;
		return execute_query($query);
	}
	
	
	function Delete($ids)
	{
		return $this->DeleteWhere($this->IdsToWhere($ids));
	}
	function DeleteWhere($where)
	{
		return execute_query("DELETE FROM ".$this->table." WHERE ".$where);
	}
	
	function DecodeValues(&$values)
	{
		$values2 = array();
		foreach($values as $f => $v)
		{
			$values2[$f] = $this->DecodeValue($f, $v);
		}
		$values = $values2;
	}
	
	function EncodeValue($f, $v)
	{
		if (@$this->field_types[$f] == "dt")
			$v = date("Y-m-d H:i:s", $v);
			
		// replace (') with ('')
		$v = str_replace("'", "''", $v);
		
		// replace (\) with (\\)
		$v = str_replace("\\", "\\\\", $v);

		return "'".$v."'";
	}
	function DecodeValue($f, $v)
	{
		if (@$this->field_types[$f] == "dt")
			return @strtotime($v);
		
		return $v;
	}
	
	function IdsToWhere($ids)
	{
		if (!is_array($ids)) $ids = array($ids);
		
		$w="FALSE";
		foreach ($ids as $id)
		{
			$w .= " OR ".$this->field_id." = ".$this->EncodeValue($this->field_id, $id);
		}
		
		return $w;
	}
}



$sql_debug_mode = 1;

// function for executing sql queries
function execute_query($q)
{
	$res = @mysql_query($q);
	
	if (mysql_errno())
	{
		$error_msg = 
			"SQL: ".$q."\n".
			"\n".
			"MySQL error code - ".mysql_errno()."\n".
			"MySQL error message: ".mysql_error();
	}
	
	if (mysql_errno())
	{
		report_error("MySQL", $error_msg);
	}
	
	global $sql_debug_mode;
	if (@$sql_debug_mode && mysql_errno())
	{
        $error_msg .=
			"\n".
			"Debug backtrace"."\n".
            var_export(debug_backtrace(), true);

		echo "<pre>$error_msg</pre>";
		die();
	}
	
	return $res;
}

?>